package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseBaseModel;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.enums.common.CommonEnum;
import com.xuecheng.common.util.StringUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.entity.CourseBase;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Collections;
import java.util.List;


/**
 * <p>
 * 课程基本信息 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class CourseBaseServiceImpl extends ServiceImpl<CourseBaseMapper, CourseBase> implements CourseBaseService {

    /*
     * 步骤分析：
     *  1.是否需要开启事务  -->不需要
     *  2.判断关键数据
     *       分页数据
     *       查询条件
     *  3.构建mp分页对象
     *  4.构建查询条件对象LambdaQueryWrapper
     *  5.查询数据
     *  6.获得数据并封装返回结果
     *       封装PageVo数据
     * */
    public PageVO queryCourseBaseList(PageRequestParams params,
                                      QueryCourseBaseModel model,
                                      long companyId) {
        //2.判断关键数据
        Long pageNo = params.getPageNo();
        Integer pageSize = params.getPageSize();
        //     分页数据
        if (ObjectUtils.isEmpty(pageNo) || pageNo<0){
            params.setPageNo(PageRequestParams.DEFAULT_PAGE_NUM);
        }

        if (ObjectUtils.isEmpty(pageSize) || pageSize<0){
            params.setPageSize(PageRequestParams.DEFAULT_PAGE_SIZE);
        }

        //     查询条件
        //构建查询条件对象LambdaQueryWrapper
        LambdaQueryWrapper<CourseBase> wrapper = new LambdaQueryWrapper<>();

        if (StringUtil.isNotBlank(model.getAuditStatus())){
            wrapper.like(CourseBase::getAuditStatus, model.getAuditStatus());
        }

        if (StringUtil.isNotBlank(model.getCourseName())){
            wrapper.eq(CourseBase::getName, model.getCourseName());
        }

        wrapper.eq(CourseBase::getCompanyId, companyId);
        wrapper.eq(CourseBase::getStatus, CommonEnum.USING_FLAG.getCode());


        //3.构建mp分页对象
        Page<CourseBase> page = new Page<>(params.getPageNo(),params.getPageSize());

        //4.构建查询条件对象LambdaQueryWrapper
        //5.查询数据
        Page<CourseBase> courseBasePage = this.page(page, wrapper);
        //6.获得数据并封装返回结果
        List<CourseBase> records = courseBasePage.getRecords();
        long total = courseBasePage.getTotal();
        List<CourseBaseDTO> dtos = Collections.EMPTY_LIST;
        if (!CollectionUtils.isEmpty(records)){
            dtos = CourseBaseConvert.INSTANCE.entitys2dtos(records);
        }
        //     封装PageVo数据
        PageVO pageVO = new PageVO(records,total,params.getPageNo(),params.getPageSize());
        return pageVO;
    }
}
